// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include <stack>
#include "commoncppproblem94.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=94 lang=cpp
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
/*
    vector<int> ans;

    vector<int> inorderTraversal(TreeNode* root)
    {
        if(root == NULL)
            return ans;
        if(root->left)
            inorderTraversal(root->left);
        ans.push_back(root->val);
        if(root->right)
            inorderTraversal(root->right);
        return ans;
    }
*/
    vector<int> inorderTraversal(TreeNode* root)
    {
        vector<int> res;    
        if(root == NULL)
            return res;
        //非递归
        stack<TreeNode*> s;

        TreeNode* p = root;
        while(p!=NULL||!s.empty())
        {
            while(p)
            {
                s.push(p);
                p = p->left;
            }
            p = s.top();
            s.pop();
            res.push_back(p->val);
            p = p->right;
        }
        return res;
    }
};
// @lc code=end

